@tool
class_name Room2D
extends Node2D

@export var margin_parent: Node2D
@export var enemy_hole_parent: EnemyHoleParent
@export var room_area:RoomArea
@export var nav_region:NavigationRegion2D
@export var room_doors:Node

var wake = false
var room_unique_name:String = ""
var map_rid:RID
var connections :Dictionary = {}


func _ready() -> void:
	if not Engine.is_editor_hint():
		_set_up()
		# 暂时在ready内调用wake_up
		#wake_up()

func _process(delta: float) -> void:
	if Engine.is_editor_hint():
		_editor_process(delta)


func wake_up():
	set_camera()
	wake = true
	enemy_hole_parent.wake_up()

func occupy():
	get_tree().call_group(room_unique_name,"occupy")

func set_camera():
	var margin:Vector4 = get_room_margin()
	var camera:Camera2D = get_viewport().get_camera_2d()
	print(margin)
	camera.limit_left  = int(margin.x)
	camera.limit_right = int(margin.y)
	camera.limit_top = int(margin.z)
	camera.limit_bottom = int(margin.w)

func get_room_margin():
	var margin :Vector4
	if not is_instance_valid(margin_parent):
		return margin
	
	var child_list = margin_parent.get_children()
	if child_list.size()>0:
		var x = child_list[0].global_position.x
		var y = child_list[0].global_position.y
		margin = Vector4(x, x, y, y)
	
	for i in child_list:
		if i.global_position.x < margin.x:
			margin.x = i.global_position.x
		if i.global_position.x > margin.y:
			margin.y = i.global_position.x
		if i.global_position.y < margin.z:
			margin.z = i.global_position.y
		if i.global_position.y > margin.w:
			margin.w = i.global_position.y
	
	return margin

func get_nav_path(src_pos,tar_pos):
	return NavigationServer2D.map_get_path(map_rid,src_pos,tar_pos,false)


func body_entered_room_area(body: Node2D) -> void:
	var last_room: String = body.get_meta("Room","NoRoom")
	if last_room != "NoRoom":
		body.remove_from_group(last_room)
	body.set_meta("Room",room_unique_name)
	body.add_to_group(room_unique_name)
	if body is Enemy_Impl:
		enemy_enter_room(body)

func enemy_enter_room(enermy:Enemy_Impl):
	if enermy is MovementEnemy:
		enermy.target_room = null


func _set_up():
	RoomManager.register_room(self)
	if is_instance_valid(nav_region):
		map_rid = nav_region.get_navigation_map()
	if is_instance_valid(room_area):
		room_area.connect("body_entered",Callable(self,"body_entered_room_area"))
	if is_instance_valid(room_doors):
		for i in room_doors.get_children():
			if i is not RoomDoor:
				continue
			i._set_up(self)


func _notification(_what: int) -> void:
	pass


func _editor_process(_delta: float) -> void:
	pass

func _get_configuration_warnings():
	var margin_warning:String = "" if is_instance_valid(margin_parent) else "未指定margin parent节点"
	if margin_warning.is_empty():
		var count = 0
		for i in margin_parent.get_children():
			if i is RoomMargin:
				count+=1
		if count < 2:
			margin_warning+="缺少RoomMargin节点"

	var hole_warning:String = "" if is_instance_valid(enemy_hole_parent) else "未指定enemy hole节点"
	if hole_warning.is_empty():
		var count = 0
		for i in enemy_hole_parent.get_children():
			if i is EnemyBornHole:
				count+=1
		if count == 0:
			hole_warning+="缺少enemy_born_hole节点"

	var door_warning:String = "" if is_instance_valid(room_doors) else "未指定此房间门的父类节点"
	if door_warning.is_empty():
		var count = 0
		for i in room_doors.get_children():
			if i is RoomDoor:
				count+=1
		if count==0:
			door_warning+="缺少门节点"

	var room_area_warning:String = "" if is_instance_valid(room_area) else "未指定房间区域检测"
	var nav_warning:String = "" if is_instance_valid(nav_region) else "未指定导航节点"

	var ret :PackedStringArray = []
	if not margin_warning.is_empty():
		ret.append(margin_warning)
	if not hole_warning.is_empty():
		ret.append(hole_warning)
	if not room_area_warning.is_empty():
		ret.append(room_area_warning)
	if not nav_warning.is_empty():
		ret.append(nav_warning)	
	if not door_warning.is_empty():
		ret.append(door_warning)
	return ret
